#ifndef APP_MAIN_CONTROLLER_H
#define APP_MAIN_CONTROLLER_H

#include "app_config_store.h"
#include "app_main_model.h"
#include "app_main_view.h"
#include "hal_rtc_device.h"
#include "hal_sensor_device.h"
#include "webapi_amap_location.h"
#include "webapi_amap_weather.h"

class AppMainController {
 public:
  AppMainController();

  /// @brief 初始化
  /// @return true 初始化成功
  bool Initialize();

  /// @brief 更新传感器状态
  /// @return true 操作成功
  bool UpdateSensorState();

  /// @brief 更新天气状态
  /// @return true 操作成功
  bool UpdateWeatherState();

  /// @brief 更新日期时间状态
  /// @return true 操作成功
  bool UpdateDateTime();

  /// @brief 显示主界面
  /// @return true 操作成功
  bool ShowMainView();

 private:
  // 配置加载、解析
  bool ShowConfigLoading();
  bool InitConfigs();
  bool LoadConfigs();
  bool SetUpAmapApiKey();

  // WiFi联网
  bool ShowWifiConnecting();
  bool ConnectToWifi(int timeout = 30000);

  // 高德定位 请求
  bool ShowLocationRequesting();
  bool RequestAmapLocation();

  // 高德天气 请求
  bool ShowWeatherRequesting();
  bool RequestAmapWeather();

  // NTP 请求
  bool ShowTimeRequesting();
  bool RequestNtpTimestamp();

  // RTC 更新
  bool ShowTimeUpdating();
  bool UpdateRtcDateTime();

  // Sensor数据更新
  bool UpdateSensorData();

  // 画布初始化
  bool InitCanvas();

 private:
  // 六大功能组件
  AppConfigStore configStore_;
  AppMainView mainView_;
  HalSensorDevice sensorDevice_;
  HalRtcDevice rtcDevice_;
  webapi::AmapLocationService locationService_;
  webapi::AmapWeatherService weatherService_;

  // 各种Model数据
  bool wifiConnected_{false};
  AppDateTime dateTime_;
  AppConfigModel configModel_;
  AppSensorModel sensorModel_;
  AppLocationModel currentLocation_;
  AppRealtimeWeather realtimeWeather_;
  AppForecastWeather forecastWeather_;
};

#endif  // APP_MAIN_CONTROLLER_H